package leetcode.treeCode.week3.leetcode_654;

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums,0, nums.length-1);
    }
    public TreeNode build (int [] nums , int left ,int right){
        if (left>right){
            return null;
        }
        int index = findMaxIndex(nums, left, right);
        TreeNode node = new TreeNode(nums[index]);
        node.left = build (nums , left , index-1);
        node.right = build (nums , index+1 , right);
        return node;
    }
    public int findMaxIndex (int [] nums , int left , int right){
        int maxIndex = left;
        for (int i = left; i <= right; i++) {
            if (nums[i]>nums[maxIndex]){
                maxIndex = i;
            }
        }
        return maxIndex;
    }
}